import React from 'react';
import { render } from 'react-dom';
import 'antd/dist/antd.css';
import { configureStore, history } from './store/configureStore';
import './index.css'
import Prompt from './components/prompt'
import Root from './containers/Root'
import { getprojecttilefilename } from './utils/savefile';
import { message, Modal } from 'antd';
import { readFileSync } from 'fs';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import { clear } from 'console';
import { appendDomToWorkspace } from './containers/methed';


const store = configureStore()


document.addEventListener('DOMContentLoaded', () =>
  render(
    <Root store={store} history={history}></Root>,
    document.getElementById('root')
  )
);


Blockly.prompt = async function (message, _default, c) {
  function useState<T>(value: T): [T, (value: T) => void] {
    function set(va: T) { value = va }
    return [value, set]
  }
  const [value, setValue] = useState(_default)
  const [visible, setVisible] = useState(true)
  const v = () => {
  }
  const data = await Prompt({ message, value, setValue, visible, setVisible }, v)
  c(data)
};
document.addEventListener('dragover', (e) => {
  e.preventDefault();
  e.stopPropagation();
})
document.addEventListener('drop', e => {
  e.preventDefault()
  e.stopPropagation()

  let file = e.dataTransfer?.files[0]
  if (file) {
    const data = getprojecttilefilename(file.name)
    if (!data) {
      return message.error('需要打开一个.xml的文件')
    }
    let result = readFileSync(file.path, 'utf8')
    Modal.confirm({
      title: '是否清空所有快？',
      icon: <ExclamationCircleOutlined />,
      okText: '确认',
      cancelText: '取消',
      onOk: () => {
        clear()
        appendDomToWorkspace(result)
      }
    });
  }
  return
})
